#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

const wregex mcWregexDash(L"\\d+-\\d+(-\\d+)*");

const vector<string> VecStrZhLastRange = {
	"范围", "比分",
};
const vector<string> VecStrEnLastRange = {
	"range", "score", "pages", "between", "year", "into", "word", "lead",
};

const vector<string> VecStrZhLastTelphone = {
	"电话", "号码", "尾号", "投诉",
};
const vector<string> VecStrEnLastTelphone = {
	"tel", "telphone", "phone", "number",
};

const vector<string> VecStrZhLastDate = {
	"出生在", "出生于",
};
const vector<string> VecStrEnLastDate = {
	"on", "born",
};

const vector<string> VecStrZhLastFormula = {
	"公式", "数学",
};
const vector<string> VecStrEnLastFormula = {
	"formula", "math",
};

const vector<string> VecStrZhUnitRangeTo = {
	"把", "瓣", "帮", "包", "本", "笔", "部", "册", "层", "场", "处", "串", "床",
	"沓", "道", "滴", "叠", "顶", "栋", "堵", "段", "堆", "对", "顿", "朵", "发", "份",
	"封", "幅", "服", "副", "杆", "个", "根", "股", "户", "伙", "家", "架", "间", "件",
	"节", "截", "届", "句", "具", "卷", "棵", "颗", "口", "块", "捆", "粒", "辆", "绺",
	"缕", "摞", "枚", "门", "面", "名", "盘", "匹", "片", "篇", "期", "群", "扇", "身",
	"首", "束", "双", "艘", "所", "台", "摊", "堂", "套", "条", "听", "头", "团", "位", 
	"只", "张", "号"
};

const vector<string> VecStrEnUnitRangeTo = {
	"degree", "degrees",
	"million", "millions",
};


const vector<string> VecStr_Zh2After_Ratio = {
	"不敌", "打败", "战胜", "领先", "超出",
	"尼克斯", "湖人", "骑士", "勇士", "火箭", "灰熊", "魔术", "爵士", "雄鹿", "活塞",
	"热火", "快船", "开拓者", "老鹰", "森林狼", "公牛", "黄蜂", "掘金", "凯尔特人", "奇才",
	"马刺", "邓肯", "帕克", "保罗",
};


void TextNormalizer::TnReDash(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while(regex_search(*fpInWsentence, tWsmatch, mcWregexDash))
	{
		string tInSentence = wstring2utf8string(*fpInWsentence);
		string tStrReplace = "";
		string tStrDash = wstring2utf8string(tWsmatch[0]);	//匹配到横杆的字符串
		vector<string> tItemDash = StrSplit(tStrDash, "-");	//按横杆分开
		size_t tItemTotal = tItemDash.size();

		//超过三项的情况，__-__-__-__，采用减法读法。
		if(tItemTotal > 3) {
			tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_subtraction:RME_zh_subtraction);
		}
		//等于3项的情况，__-__-__，采用减法读法。
		else if(tItemTotal == 3) {
			//提取数字前面的字符串
			const vector<wstring> tcZhVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_ZH);
			const vector<wstring> tcEnVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_EN);

			//排除一定不是日期（2023-8-16）的情况
			bool tMaybeDateFlag = CheckIsDateNianyueri(tItemDash);

			int tCountDate = FindVectorPatternCount(tcZhVecStrLastWords, VecStrZhLastDate, LANG_ZH)
							 + FindVectorPatternCount(tcEnVecStrLastWords, VecStrEnLastDate, LANG_EN);
			int tCountTelphone = FindVectorPatternCount(tcZhVecStrLastWords, VecStrZhLastTelphone, LANG_ZH)
							 + FindVectorPatternCount(tcEnVecStrLastWords, VecStrEnLastTelphone, LANG_EN);
			int tCountFormula = FindVectorPatternCount(tcZhVecStrLastWords, VecStrZhLastFormula, LANG_ZH)
							 + FindVectorPatternCount(tcEnVecStrLastWords, VecStrEnLastFormula, LANG_EN);
			if(tMaybeDateFlag && tCountDate >= tCountTelphone && tCountDate >= tCountFormula) {
				tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_chinesedate:RME_zh_chinesedate);
			}
			else if(tCountTelphone >= tCountFormula && tCountTelphone >= tCountDate) {
				tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_telephone);
			}
			else {
				tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_subtraction:RME_zh_subtraction);
			}
		}
		//等于2项的情况，__-__
		else if(tItemTotal == 2) {
			//如果数字后面有单位，则按范围读
			const vector<wstring> tcZhVecStrNextWords = GetWstrPositionNextWords(*fpInWsentence, tWsmatch.position(0) + tWsmatch.length(0), 2, LANG_ZH);
			const vector<wstring> tcEnVecStrNextWords = GetWstrPositionNextWords(*fpInWsentence, tWsmatch.position(0) + tWsmatch.length(0), 1, LANG_EN);

			const string tcZh1After = tcZhVecStrNextWords.size()>0 ? wstring2utf8string(tcZhVecStrNextWords[0]) : "";
			const string tcEn1After = tcEnVecStrNextWords.size()>0 ? wstring2utf8string(tcEnVecStrNextWords[0]) : "";

			const string tcZh2After = tcZhVecStrNextWords.size()>1 ? wstring2utf8string(tcZhVecStrNextWords[0] + tcZhVecStrNextWords[1]) : "";

			if(tcZh1After == "年" || tcZh2After == "赛季") {
				for(size_t i=0; i<tItemDash.size(); i++) {
					if(i>0) {
						tStrReplace += (fcInLanguage==LANG_EN) ? " to " : "至";
					}
					tStrReplace += ReplaceMethod(vector<string>{tItemDash[i]}, (fcInLanguage==LANG_EN)?RME_en_year:RME_zh_digit_yi1);
				}
			}
			else if(tItemDash[0].length() >= 2 && tItemDash[0][0] == '0')			//如果第一个数字是0，则按电话号码读
			{
				tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_telephone);
			}
			else if(tcZh2After!="" && VecStr_Zh2After_Ratio.end() != find(VecStr_Zh2After_Ratio.begin(), VecStr_Zh2After_Ratio.end(), tcZh2After))
			{
				tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_colon_ratio:RME_zh_colon_ratio);
			}
			else if(tcZh1After!="" && VecStrZhUnitRangeTo.end() != find(VecStrZhUnitRangeTo.begin(), VecStrZhUnitRangeTo.end(), tcZh1After) )
			{
				tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_range:RME_zh_range);
			}
			else if(tcEn1After!="" && VecStrEnUnitRangeTo.end() != find(VecStrEnUnitRangeTo.begin(), VecStrEnUnitRangeTo.end(), tcEn1After) )
			{
				tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_range:RME_zh_range);
			}
			else
			{
				//提取数字前面的字符串
				const vector<wstring> tcZhVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_ZH);
				const vector<wstring> tcEnVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_EN);

				//如果存在电话类型关键词，则以电话读法
				int tCountTelphone = FindVectorPatternCount(tcZhVecStrLastWords, VecStrZhLastTelphone, LANG_ZH)
									 + FindVectorPatternCount(tcEnVecStrLastWords, VecStrEnLastTelphone, LANG_EN);
				//如果存在范围类型关键词，则以范围读法
				int tCountRange = FindVectorPatternCount(tcZhVecStrLastWords, VecStrZhLastRange, LANG_ZH)
								 + FindVectorPatternCount(tcEnVecStrLastWords, VecStrEnLastRange, LANG_EN);
				//如果存在公式等其它，则以减法读法
				int tCountFormula = FindVectorPatternCount(tcZhVecStrLastWords, VecStrZhLastFormula, LANG_ZH)
								 + FindVectorPatternCount(tcEnVecStrLastWords, VecStrEnLastFormula, LANG_EN);

				if(tCountRange >= tCountTelphone && tCountRange >= tCountFormula) {
					tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_range:RME_zh_range);
				}
				else if(tCountTelphone >= tCountRange && tCountTelphone >= tCountFormula) {
					tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_telephone);
				}
				else {
					tStrReplace = ReplaceMethod(tItemDash, (fcInLanguage==LANG_EN)?RME_en_subtraction:RME_zh_subtraction);
				}
			}
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}
